#include <stdlib.h>
#include "wifi.h"
#include "zlog.h"
static int count = 0;

void wifiApCb(List *list)
{
    int i = 0;
    for (i = 0; i < list_count(list); i++) {
        ScanAPInfo *ap = list_data(list, i);
        if (NULL != ap)
            printf("keyopen:(%s)%d %d\n", ap->ssid, ap->Keyopen, ap->level);
    }
    count++;
    if (3 == count) {
        exit(0);
    }
}

void wifiAutoConnCb(char *ssid) { printf("ssid %s\n", ssid); }

int main(int argc, char *argv[])
{
    int ret   = -1;
    int level = 0;
    char *wifi_mac, *wifi_ip, *psk, *wifi_mask, *wifi_gw, *ssid;
    int select = 0, security = AUTH_WPA2_PSK;

    int zret = dzlog_init("/etc/xp03_log.conf", "xp03_cat");
    if (zret) {
        printf("zlog init failed\n");
        exit(0);
    }

    if (0 > redbee_wifi_init()) {
        printf("wifi init error\n");
        return -1;
    }

    if (argc < 2) {
        printf(
            "please use wifi [option]\n  \
                option:\n    \
                scan\n   \
                connect [ssid] [passcode [security(WPA2)]\n  \
                disconnect\n \
                mac\n    \
                ip\n");
        return -2;
    }

    if (0 == strcmp(argv[1], "scan"))
        select = 1;
    else if (0 == strcmp(argv[1], "connect"))
        select = 2;
    else if (0 == strcmp(argv[1], "disconnect"))
        select = 3;
    else if (0 == strcmp(argv[1], "select"))
        select = 4;
    else if (0 == strcmp(argv[1], "remove"))
        select = 5;
    else if (0 == strcmp(argv[1], "list"))
        select = 6;
    else if (0 == strcmp(argv[1], "state"))
        select = 7;
    else if (0 == strcmp(argv[1], "signal"))
        select = 8;
    else if (0 == strcmp(argv[1], "psk"))
        select = 9;
    else if (0 == strcmp(argv[1], "wifi_mac"))
        select = 10;
    else if (0 == strcmp(argv[1], "wifi_ip"))
        select = 11;
    else if (0 == strcmp(argv[1], "wifi_mask"))
        select = 12;
    else if (0 == strcmp(argv[1], "wifi_gw"))
        select = 13;
    else if (0 == strcmp(argv[1], "auto"))
        select = 14;

    switch (select) {
        case 1:  // scan AP signal
            redbee_wifi_scan(wifiApCb, 5, wifiAutoConnCb, 5);
#if 1
            while (1) {
                int scanf_v = 0;
                scanf("%d", &scanf_v);
                switch (scanf_v) {
                    case 1:
                        redbee_wifi_change_scanAP(SCAN_SEC, 3);
                        break;
                    case 2:
                        redbee_wifi_change_scanAP(CONNECT_SEC, 5);
                        break;
                    case 3:
                        redbee_wifi_change_scanAP(SCAN_SEC, 0);
                        break;
                    case 4:
                        redbee_wifi_change_scanAP(CONNECT_SEC, 0);
                        break;
                    default:
                        break;
                }
            }
#else
            while (1) {
            };
#endif
            break;

        case 2:
            if (argc < 5) {
                printf(
                    "want to connect, but parameter is less than 5\n \
                        please use: \n  \
                        wifi connect [ssid] [passwod] [security(WPA/WPA2)]\n");
            } else {
                if (!strcmp("WPA2_PSK", argv[4]))
                    security = AUTH_WPA2_PSK;
                else if (!strcmp("WPA2_EAP", argv[4]))
                    security = AUTH_WPA2_EAP;
                else if (!strcmp("WPA_PSK", argv[4]))
                    security = AUTH_WPA_PSK;
                else if (!strcmp("WPA_EAP", argv[4]))
                    security = AUTH_WPA_EAP;
                else if (!strcmp("WEP", argv[4]))
                    security = AUTH_NONE_WEP;
                else if (!strcmp("WEP_SHARED", argv[4]))
                    security = AUTH_NONE_WEP_SHARED;
                else if (!strcmp("IEEE", argv[4]))
                    security = AUTH_IEEE8021X;
                else if (!strcmp("OPEN", argv[4]))
                    security = AUTH_NONE_OPEN;
                ret = redbee_wifi_connectNewAP(argv[2], argv[3], security, 20);
                printf("wifi connect to wifi %s success(%d)\n", argv[2], ret);
            }
            break;

        case 3:  // disconnect the current AP
            redbee_wifi_disconnect();
            break;

        case 4:  // connect a AP which is stored in config file
            ret = redbee_wifi_connectOldAP(argv[2], 15);
            printf("wifi select wifi %s success(%d)\n", argv[2], ret);
            break;

        case 5:  // remove AP from config file
            ret = redbee_wifi_removeSelectAP(argv[2]);
            break;

        case 6:  // get store AP list
            ret = redbee_wifi_netListWorks("Syber-sense");
            if (ret >= 0)
                printf("ssid is find(%d)-----\n", ret);
            break;

        case 7:  // get connect state
            ssid = redbee_wifi_connectSsid();
            printf("connected ssid:%s\n", ssid);
            break;

        case 8:  // get signal level
            ret = redbee_wifi_get_signal("Syber-sense", &level);
            printf("signal level(%d):%d\n", ret, level);
            break;

        case 9:  // get the passward of AP whih is connected
            ret = redbee_wifi_get_psk(argv[2], &psk);
            printf("config_file:%c--%c\n", psk[0], psk[1]);
            break;
        case 10:  // get MAC address of current AP
            wifi_mac = redbee_wifi_getMac();
            printf("wifi mac address: %2x:%2x:%2x:%2x:%2x:%2x\n", wifi_mac[0], wifi_mac[1], wifi_mac[2], wifi_mac[3], wifi_mac[4], wifi_mac[5]);
            break;

        case 11:  // get ipaddress
            wifi_ip = redbee_getIpAddr(ETH_NAME);
            if (NULL != wifi_ip)
                printf("current ip address: %s\n", wifi_ip);
            break;

        case 12:  // get MAC address of current AP
            wifi_mask = redbee_wifi_getNetMask();
            printf("wifi mask address:%s\n", wifi_mask);
            break;

        case 13:  // get ipaddress
            wifi_gw = redbee_wifi_getGateway();
            printf("wifi gw address: %s\n", wifi_gw);
            break;

        case 14:
            // EnableAllList();
            // wifi_autoConn(wifiAutoConnCb,5);
            break;
        case 15:
            redbee_wifi_close();
            break;
    }
#if 0
    if (!zret) {
        zlog_fini();
    }
#endif
    redbee_wifi_close();
    return 0;
}
